//
//  DiseaseMedicalViewController.m
//  FastDoctor
//
//  Created by 李通 on 16/7/8.
//  Copyright © 2016年 sc. All rights reserved.
//

#import "DiseaseMedicalViewController.h"
#import "Use.h"
#import "DetailDiseaseController.h"
#import "EruptController.h"
#import "NetWorkTool.h"
#import <AFHTTPSessionManager.h>
#import <AFNetworking.h>


#import "MedicalTypeController.h"
#import "FocuseDoctorViewController.h"
//define this constant if you want to use Masonry without the 'mas_' prefix
#define MAS_SHORTHAND

//define this constant if you want to enable auto-boxing for default syntax
#define MAS_SHORTHAND_GLOBALS

#define titleColor [UIColor colorWithRed:214/255.0 green:213/255.0 blue:217/255.0 alpha:1];




#define kFilePath(fileName) ([NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:fileName])

@interface DiseaseMedicalViewController ()
@property(nonatomic,weak)UIButton *confirmButton1;
@property(nonatomic,weak)UIButton *confirmButton2;
@property(nonatomic,weak)UIButton *patientButton1;
@property(nonatomic,weak)UIButton *patientButton2;
@property(nonatomic,weak)UIView *thirdView;
@property(nonatomic,weak)UITextField *firstField;
@property(nonatomic,weak)UITextField *secondField;
@property(nonatomic,weak)UILabel *diseaseLabel;
@property(nonatomic,weak)UILabel *thirdLabel;
@property(nonatomic,weak)UILabel *fourthLabel;
@property(nonatomic,weak)UITextField *fiveField;
@property(nonatomic,weak)UIButton *bottonButton;
@property (nonatomic,weak) UIView *firstView;
@property(nonatomic,weak)UIView *fourthView;


@end

@implementation DiseaseMedicalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor =  [UIColor colorWithRed:241/255.0 green:240/255.0 blue:247/255.0 alpha:1];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"home_nav_button_back"] style:UIBarButtonItemStylePlain target:self action:@selector(goreturn)];
    self.navigationItem.leftBarButtonItem = leftItem;
    leftItem.tintColor = [UIColor whiteColor];
;
    
    [self setupUI];
    [self test7];
    
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self read];
}


-(void)goreturn{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)setupUI{
    
    Use *use =[[Use alloc]init];
    //用户信息
    use.personInfo = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath(@"user.data")];
    UILabel *userLabel = [[UILabel alloc]init];
    [self.view addSubview:userLabel];
    [userLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(80);
        make.left.offset(10);
    }];
    [userLabel sizeToFit];
    
    userLabel.text = [NSString stringWithFormat:@"用户信息: %@",use.personInfo[@"true_name"]];
    
    UILabel *diseaseLabel= [[UILabel alloc]init];
    [self.view addSubview:diseaseLabel];
    [diseaseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(userLabel).offset(30);
        make.left.equalTo(userLabel);
        
    }];
    [diseaseLabel sizeToFit];
    diseaseLabel.text = [NSString stringWithFormat:@"疾病类型: %@",self.title];
    
    //第一个Field

    UITextField *firstField = [self setSelectedView];
     firstField.placeholder = @"请选择疾病细分";
    self.firstField = firstField;
    [self.view addSubview:firstField];
   
    if (kDevice_Is_iPhone5 ) {
        [firstField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(diseaseLabel.mas_bottom).offset(20);
            make.left.equalTo(diseaseLabel);
            make.right.offset(-10);
            make.height.equalTo(@30);
        }];
    } else{
    [firstField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(diseaseLabel.mas_bottom).offset(20);
        make.left.equalTo(diseaseLabel);
        make.right.offset(-10);
        make.height.equalTo(@44);
    }];
    }
    
    UIView *coverView = [self getCoverviewWith:firstField];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(jumpnext)];
    [coverView addGestureRecognizer:tap];
    
    //第二个field
    UITextField *secondField = [self setSelectedView];
    self.secondField = secondField ;

    secondField.placeholder =@"请选择病发症 :(可多选)";
    NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath(@"chose.data")];
    if (array) {
        NSMutableString *mStr = [NSMutableString string];
        
        for (NSString *str in array) {
            //拼接字符串用于区分多个并发症！！！！勿改！！
            [mStr appendString:[NSString stringWithFormat:@"%@、",str]];
        }
        if (mStr.length >1) {
            secondField.text = [mStr substringToIndex:mStr.length-1];
        }
    }
    [self.view addSubview:secondField];
    if (kDevice_Is_iPhone5) {
        [secondField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(firstField.mas_bottom).offset(20);
            make.left.equalTo(firstField);
            make.right.offset(-10);
            make.height.equalTo(@30);
        }];
        
    }else{
    
    [secondField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(firstField.mas_bottom).offset(20);
        make.left.equalTo(firstField);
        make.right.offset(-10);
        make.height.equalTo(@44);
    }];
    }
 
    
    UIView *coverView1 = [self getCoverviewWith:secondField];
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(jumpnextt)];
    [coverView1 addGestureRecognizer:tap1];
    
    
    
    //第三个view
    
    UIButton *confirmButton1 = [[UIButton alloc]init];
    confirmButton1.tag =1;
    self.confirmButton1 =confirmButton1;
    
    UIButton *confirmButton2 = [[UIButton alloc]init];
    self.confirmButton2 = confirmButton2;
    confirmButton2.tag =2;
    
    UILabel *thirdLabel = [[UILabel alloc]init];
    thirdLabel.textColor = titleColor;
    self.thirdLabel = thirdLabel;
    thirdLabel.text = @"是否就诊";
    UIView *thirdView = [self setChooseViewWith:thirdLabel andsecondStr:@"已确诊" andthirdStr:@"症状疑似" andthirdButton:confirmButton1 andFourthButton:confirmButton2];
    
    self.thirdView = thirdView;
    [self.view addSubview:thirdView];
    if (kDevice_Is_iPhone5) {
        [thirdView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(secondField.mas_left);
            make.top.equalTo(secondField.mas_bottom).offset(20);
            make.right.offset(-10);
            make.height.equalTo(@30);
        }];
        
    }else{
    [thirdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(secondField.mas_left);
        make.top.equalTo(secondField.mas_bottom).offset(20);
        make.right.offset(-10);
        make.height.equalTo(@44);
    }];
    }

    //第四个view
    UIButton *patientButton1 = [[UIButton alloc]init];
    self.patientButton1 = patientButton1;
    patientButton1.tag =3;
    UIButton *patientButton2 = [[UIButton alloc]init];
    self.patientButton2 =patientButton2;
    patientButton2.tag =4;
   
    UILabel *fourthLabel =[[UILabel alloc]init];
    fourthLabel.textColor = titleColor;
    self.fourthLabel = fourthLabel;
    fourthLabel.text =@"是否接受过治疗";
    
    UIView *fourthView = [self setChooseViewWith:fourthLabel andsecondStr:@"接受过" andthirdStr:@"未接受过" andthirdButton:patientButton1 andFourthButton:patientButton2];
    
    [self.view addSubview:fourthView];
    if (kDevice_Is_iPhone5) {
        [fourthView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(thirdView.mas_left);
            make.top.equalTo(thirdView.mas_bottom).offset(20);
            make.right.offset(-10);
            make.height.equalTo(@30);
        }];
        
    }else{
    [fourthView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(thirdView.mas_left);
        make.top.equalTo(thirdView.mas_bottom).offset(20);
        make.right.offset(-10);
        make.height.equalTo(@44);
    }];
    }
    
    self.fourthView = fourthView;
    
    //底层button
    UIButton *bottomButton = [[UIButton alloc]init];
    [self.view addSubview:bottomButton];
    [bottomButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-10);
        make.left.offset(8);
        make.right.offset(-8);
        make.height.equalTo(@44);
    }];
    bottomButton.backgroundColor = [UIColor colorWithRed:0/255.0 green:199/255.0 blue:212/255.0 alpha:1];
    [bottomButton setTitle:@"就医申请" forState:UIControlStateNormal];
    [bottomButton addTarget:self action:@selector(didClickMedical) forControlEvents:UIControlEventTouchUpInside];
    self.bottonButton = bottomButton;
    // 医生头像
    UIImageView *img = [[UIImageView alloc]init];
    [self.view addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(bottomButton.mas_top).offset(-30);
        make.centerX.equalTo(self.view);
        make.height.width.equalTo(@80);
    }];
    img.image = [UIImage imageNamed:@"illness_img_person.png"];
}


-(void)didclickThirdButton:(UIButton *)thirdButton{
    thirdButton.selected = ![thirdButton isSelected];
    if (thirdButton.tag ==1) {
        
        
        self.confirmButton2.selected =![thirdButton isSelected];
        
        [self getChangeLabelWithButton:self.confirmButton2 andFirstStr:@"症状疑似" andLabel:_thirdLabel andview:_thirdView andString:@"已确诊"];
    }
    
    if (thirdButton.tag == 3) {
        self.patientButton2.selected =![thirdButton isSelected];
        [self getChangeLabelWithButton:self.patientButton1 andFirstStr:@"接受过" andLabel:_fourthLabel andview:_fourthView andString:@"未接受过"];
    }
    
    [self getLabelTextByView:_thirdView WithButton:_confirmButton1 andbool:0 withfirstStr:@"症状疑似" andsecondStr:@"是否就诊"];
    
    
    [self getLabelTextByView:_fourthView WithButton:_patientButton1 andbool:1 withfirstStr:@"接受过" andsecondStr:@"是否接受过治疗"];
    
    if ([self.confirmButton1 isSelected] && [self.patientButton1 isSelected] &&self.secondField.text.length !=0){
        [self getFifthView];
    }

    
    [self save];
    
}
-(void)didclickFourthButton:(UIButton *)fourthButton{
    fourthButton.selected = ![fourthButton isSelected];
    
    if (fourthButton.tag == 2) {
        self.confirmButton1.selected =![fourthButton isSelected];
        
        [self getChangeLabelWithButton:self.confirmButton2 andFirstStr:@"症状疑似" andLabel:_thirdLabel andview:_thirdView andString:@"已确诊"];
    }
    if (fourthButton.tag ==4) {
        
        self.patientButton1.selected =![fourthButton isSelected];
        
        [self getChangeLabelWithButton:self.patientButton2 andFirstStr:@"未接受过" andLabel:_fourthLabel andview:_fourthView andString:@"接受过"];
    }
    
    [self getLabelTextByView:_thirdView WithButton:_confirmButton2 andbool:1 withfirstStr:@"症状疑似" andsecondStr:@"是否就诊"];
    
    [self getLabelTextByView:_fourthView WithButton:_patientButton2 andbool:0 withfirstStr:@"接受过" andsecondStr:@"是否接受过治疗"];
    
    if ([self.confirmButton1 isSelected ] && [self.patientButton1 isSelected] &&self.secondField.text.length !=0){
        [self getFifthView];
    }
    [self save];
    
    
}





//********************************控制器跳转实现********************************
//选择疾病细分跳转
-(void)jumpnext{
    
    DetailDiseaseController *dc = [[DetailDiseaseController alloc]init];
    
    dc.block = ^(NSString *cellText){
        
        self.firstField.text = cellText;
    };
    
    [self.navigationController pushViewController:dc animated:YES];
}

//选择并发症跳转
-(void)jumpnextt{
    EruptController *er = [[EruptController alloc]init];
    er.title = @"并发症";
    er.block = ^(NSString *string) {
        
        self.secondField.text = string;
        self.confirmButton1.selected = 1;
        self.confirmButton2.selected = 0;
        self.patientButton1.selected = 0;
        self.patientButton2.selected = 1;
        
        self.confirmButton1.selected = YES;
        self.confirmButton2.selected = NO;
    };
    
    [self.navigationController pushViewController:er animated:YES];
 
}

//跳转到治疗方式
- (void)jumpToMedicalType {
    
    MedicalTypeController *medicalTypeVC = [[MedicalTypeController alloc]init];
    
    medicalTypeVC.title = @"治疗方式";
    
    medicalTypeVC.block = ^(NSString *string){
        
        self.fiveField.text = string;
    };
    [self.navigationController pushViewController:medicalTypeVC animated:YES];
}

//跳转到就医申请
- (void)didClickMedical {
    
    FocuseDoctorViewController *focuseVC = [[FocuseDoctorViewController alloc]init];
    
    focuseVC.title = @"选择医生";
    
    [self.navigationController pushViewController:focuseVC animated:YES];
}
//********************************控制器跳转实现********************************


-(UITextField *)setSelectedView{
    UITextField *field = [[UITextField alloc]init];
    UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
    field.leftView =leftView;
    field.leftViewMode = UITextFieldViewModeAlways;
    field.backgroundColor = [UIColor whiteColor];
    UIView *view = [[UIView alloc]init];
    field.userInteractionEnabled = YES;
    [field addSubview:view];
    
    if (kDevice_Is_iPhone5) {
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(0);
            make.left.offset(275);
            make.height.equalTo(@30);
            make.right.offset(-10);
        }];
    }else{
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.left.offset(275);
        make.height.equalTo(@44);
        make.right.offset(-10);
    }];
    }
    
    
    UIButton *firstButton = [[UIButton alloc]init];
    [view addSubview:firstButton];
    firstButton.enabled = NO;
    [firstButton setBackgroundImage:[UIImage imageNamed:@"illness_button_input_blue.png"] forState:UIControlStateNormal];
    [firstButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-10);
        make.centerY.equalTo(field);
        make.width.equalTo(@15);
        make.height.equalTo(@15);
    }];
    
    [firstButton addTarget:self action:@selector(jumpnext) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *linView = [[UIView alloc]init];
    [view addSubview:linView];
    [linView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(firstButton.mas_left).offset(-20);
        make.width.equalTo(@1);
        make.top.offset(10);
        make.bottom.offset(-10);
    }];
    linView.backgroundColor = titleColor;
    
    return field;
    
}

-(UIView*)setChooseViewWith:(UILabel *)label andsecondStr :(NSString *)secondStr andthirdStr :(NSString *)thirdStr  andthirdButton :(UIButton*)thirdButton andFourthButton :(UIButton *)fourthButton{
    UIView *view = [[UIView alloc]init];
    
    view.backgroundColor = [UIColor whiteColor];
    
    [view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.left.offset(10);
        make.right.equalTo(view.mas_centerX);
    }];
    [view addSubview:thirdButton];
    [thirdButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view);
        make.left.equalTo(view.centerX).offset(-10);
        
    }];
    [thirdButton setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
    [thirdButton setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
    [thirdButton addTarget:self action:@selector(didclickThirdButton:) forControlEvents:UIControlEventTouchUpInside];
    UILabel *secondLabel = [[UILabel alloc]init];
    [view addSubview:secondLabel];
    [secondLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(thirdButton.mas_right).offset(3);
        make.top.bottom.offset(0);
        
    }];
    [secondLabel sizeToFit];
    secondLabel.text = secondStr;
    
    [view addSubview:fourthButton];
    [fourthButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(secondLabel.mas_right).offset(0);
        make.centerY.equalTo(view);
    }];
    [fourthButton setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
    [fourthButton setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
    
    [fourthButton addTarget:self action:@selector(didclickFourthButton:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UILabel *thirdLabel = [[UILabel alloc]init];
    [view addSubview:thirdLabel];
    [thirdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(fourthButton.mas_right).offset(3);
        make.top.bottom.offset(0);
        
    }];
    [thirdLabel sizeToFit];
    thirdLabel.text = thirdStr;
    
    
    return view;
}


-(void)getLabelTextByView :(UIView*)view WithButton : (UIButton *)button andbool :(BOOL) num withfirstStr :(NSString *)firstStr andsecondStr :(NSString *)secondStr{
    [view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[UILabel class]]) {
            UILabel *label = obj;
            if (idx ==0) {
                
                if ([button isSelected] == num) {
                    label.text = firstStr;
                    
                }
                else{
                    label.text = secondStr;
                }
                
            }
        }
    }];
}

-(UIView*)getCoverviewWith :(UITextField*)field{
    UIView *coverView = [[UIView alloc]init];
    coverView.backgroundColor = [UIColor clearColor];
    [field addSubview:coverView];
    [coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.offset(0);
    }];
    return coverView;
}

-(void)getChangeLabelWithButton :(UIButton *)button andFirstStr :(NSString *)firstStr andLabel :(UILabel *)label  andview:(UIView *)view andString :(NSString*)str{
    if (self.secondField.text.length !=0) {
        if ([button isSelected]) {
            
            label.textColor = [UIColor blackColor];
    
    label.text = firstStr;
    [view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.left.offset(10);
        make.right.equalTo(view.mas_centerX);
    }];
}
else{
   label.text = str;
         }
     }
}

-(void)getFifthView{
        UITextField *field =  [self setSelectedView];
        self.fiveField = field;
        field.placeholder = @"请选择治疗方式";
        [self.view addSubview:field];
    if (kDevice_Is_iPhone5) {
        [field mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_fourthView.mas_left);
            make.top.equalTo(_fourthView.mas_bottom).offset(20);
            make.right.offset(-10);
            make.height.equalTo(@30);
        }];
        
        
    }else{
        [field mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_fourthView.mas_left);
            make.top.equalTo(_fourthView.mas_bottom).offset(20);
            make.right.offset(-10);
            make.height.equalTo(@44);
        }];
    }
   
        
        UIView *coverView = [self getCoverviewWith:field];
        
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(jumpToMedicalType)];
        [coverView addGestureRecognizer:tap1];
    
        
    
}

-(void)test7{
    NSString *urlString = kMatchDoctors;
    NSDictionary *paramDic = @{@"ci1_id":@1,
                               @"ci2_id":@3,
                               @"ci3_id":@3,
                               @"diagnosis_type":@0,
                               @"page_size":@15,
                               @"is_confirmed":@1,
                               @"user_id":@1000089,
                               @"page":@1,
                               @"has_diagnosis":@(2)
                               };
    [self loadUrl:urlString params:paramDic];
    }

-(void)loadUrl:(NSString *)urlString params:(NSDictionary *)paramDic{
    
    [[NetWorkTool sharedWorkTool]POST:urlString parameters:paramDic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *countArray = responseObject[@"data"];
        UILabel *label = [[UILabel alloc]init];
        [self.view addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view.centerX);
            make.bottom.equalTo(self.bottonButton.top).offset(-10);
        }];
        [label sizeToFit];
        label.text = [NSString stringWithFormat:@"为您匹配到: %ld名医生",countArray.count];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error");
    }];

     }
-(void)read{
    NSDictionary *dict = [[NSUserDefaults standardUserDefaults]objectForKey:@"buttonState"];
    if (self.confirmButton1.selected==0&&self.confirmButton2.selected ==0&&self.patientButton1.selected ==0&&self.patientButton2.selected ==0) {
        self.confirmButton1.selected = 1;
        self.patientButton2.selected =1;
    }

    self.confirmButton1.selected = [dict[@"confirmButton1isSelected"] intValue];
    self.confirmButton2.selected = [dict[@"confirmButton2isSelected"] intValue];
    self.patientButton1.selected = [dict[@"patientButton1isSelected"] intValue];
    self.patientButton2.selected = [dict[@"patientButton2isSelected"] intValue];
    
    }

-(void)save{
     NSNumber *state1 = [NSNumber numberWithBool:[self.confirmButton1 isSelected]];
    NSNumber *state2 = [NSNumber numberWithBool:[self.confirmButton2 isSelected]];
    NSNumber *state3 = [NSNumber numberWithBool:[self.patientButton1 isSelected]];
    NSNumber *state4 = [NSNumber numberWithBool:[self.patientButton2 isSelected]];
    NSUserDefaults *defaults  = [NSUserDefaults standardUserDefaults];
    [defaults setObject:@{@"confirmButton1isSelected":state1,@"confirmButton2isSelected":state2,@"patientButton1isSelected":state3,@"patientButton2isSelected":state4} forKey:@"buttonState"];
    [defaults synchronize];
    
}

@end
